<?php

namespace App\Http\Controllers\Home;

use App\Models\Home\UserInfo;
use App\Models\Home\Weibo;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;

class TopicController extends Controller
{
    public function show($topic_id)
    {
        //所有微博
        $data = Weibo::leftJoin('user_infos', 'user_infos.user_id', '=', 'weibos.user_id')
            ->select('weibos.*', 'user_infos.user_id', 'user_infos.nickname', 'user_infos.avatar')
            ->where('weibos.topic_id', $topic_id)
            ->where('weibos.is_show', 1)
            ->get()
            ->toArray();

        //当前用户id
        $user_id = Session::get('user.id');
        if ($user_id) {
            //获取收藏点赞信息
            $a = UserInfo::select('favorites', 'likes')
                ->where('user_id', '=', $user_id)
                ->first()
                ->toArray();

            //全部微博中增加收藏点赞标识
            foreach ($data as $k => $v) {
                $data[$k]['favorites'] = $a['favorites'];
                $data[$k]['likes'] = $a['likes'];
            }
        }
        return view('home.topic', ['feeds' => $data]);
    }
}
